//
//  RecommendPrettyViewCell.swift
//  SwiftDemo
//
//  Created by liguoqing on 2022/6/29.
//  Copyright © 2022 guoqing.li. All rights reserved.
//

import UIKit
import Kingfisher

class RecommendPrettyViewCell: UICollectionViewCell
{
    private lazy var imgView : UIImageView = UIImageView()
    
    private lazy var onlineBtn : UIButton = {
        let onlineBtn = UIButton(type: .custom)
        onlineBtn.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
        onlineBtn.setTitleColor(UIColor.white, for: .normal)
        onlineBtn.titleLabel?.font = .systemFont(ofSize: 11)
        onlineBtn.isUserInteractionEnabled = false
        onlineBtn.layer.cornerRadius = 4.0
        return onlineBtn
    }()
    
    private lazy var nameLabel : UILabel = {
        let nameLabel = UILabel()
        nameLabel.textColor = .black
        nameLabel.font = .systemFont(ofSize: 13)
        return nameLabel
    }()
    
    private lazy var localBtn : UIButton = {
        let localBtn = UIButton(type: .custom)
        localBtn.setImage(UIImage(named: "ico_location"), for: .normal)
        localBtn.setTitleColor(UIColor.lightGray, for: .normal)
        localBtn.titleLabel?.font = .systemFont(ofSize: 11)
        return localBtn
    }()
    
    var anchor: AnchorModel? {
        didSet{
            //0. 校验模型是否有值
            guard let anchor = anchor else { return }
            
            //1. 取出在线人数显示的文字
            var onlineStr : String = ""
            if anchor.online >= 10000 {
                onlineStr = "\(Int(anchor.online / 10000))万在线"
            } else {
                onlineStr = "\(anchor.online)在线"
            }
            onlineBtn.setTitle(onlineStr, for: .normal)
            
            //2. 昵称显示
            nameLabel.text = anchor.nickname
            
            //3. 设置图片封面
            guard let iconURL = NSURL(string: anchor.vertical_src) else { return }
            
            let url = ImageResource(downloadURL: iconURL as URL)
            imgView.kf.setImage(with: url)
            
            localBtn.setTitle(anchor.anchor_city, for: .normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension RecommendPrettyViewCell
{
    private func setupUI(){
        contentView.addSubview(imgView)
        contentView.addSubview(onlineBtn)
        contentView.addSubview(nameLabel)
        contentView.addSubview(localBtn)
        imgView.snp.makeConstraints { make in
            make.leading.top.trailing.equalTo(0)
            make.height.equalTo(165)
        }
        onlineBtn.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.trailing.equalTo(-5)
            make.height.equalTo(20)
        }
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(imgView.snp.bottom)
            make.leading.trailing.equalTo(0)
            make.height.equalTo(20)
        }
        localBtn.snp.makeConstraints { make in
            make.top.equalTo(nameLabel.snp.bottom)
            make.leading.equalTo(0)
            make.height.equalTo(14)
        }
    }
}
